/**
 * @file app.js
 * @author swan
 */

/* globals swan */
App({
    globalData:{
        userInfo:null,
        url:'http://02.hbcms.eiewz.cn/m/',
        urlapi:'http://02.hbcms.eiewz.cn/api/index.php/BDAPI/BDAPI/',
        urllogin:'http://02.hbcms.eiewz.cn/api/index.php/BDAPI/BDAPI/'
    },
    onLaunch(options) {
        this.gocheck();
        // do something when launch
        // 引导添加，参见文档： http://smartprogram.baidu.com/docs/design/component/guide_add/
        // if (swan.canIUse('showFavoriteGuide')) {
        //     swan.showFavoriteGuide({
        //         type: 'bar',
        //         content: '一键关注小程序',
        //         success(res) {
        //             console.log('关注成功：', res);
        //         },
        //         fail(err) {
        //             console.log('关注失败：', err);
        //         }
        //     });
        // }
    },
    gocheck(){
        const that = this;
        swan.checkSession({ //检查登录是否过期
            success(){
                swan.getStorage({
                    key: 'login_data',
                    success: res => {
                        console.log(res.data)
                        if(!res.data){
                            that.getopenid(function(res){
                                swan.setStorage({
                                    key: 'login_data',
                                    data: res,
                                    success:e=>{
                                        that.relateUser(e)
                                    }
                                });
                            });
                        }else{
                            // console.log('有login_data')
                            that.relateUser(res.data); 
                        }
                    },
                    fail(){
                        that.getopenid(function(res){
                            swan.setStorage({
                                key: 'login_data',
                                data: res,
                                success:e=>{
                                    that.relateUser(res)
                                }
                            });
                        });
                    }
                });
            },
            fail(){
                // console.log('登录过期');
                that.getopenid(function(res){
                    swan.setStorage({
                        key: 'login_data',
                        data: res,
                        success:e=>{
                            that.relateUser(res)
                        }
                    });
                });
            },
        });
    },
    getopenid(callback){  //获取登录的login_data
        const that = this;
        swan.login({
            success(data){
                swan.request({
                    url: that.globalData.urllogin + 'onLogin',
                    data:{
                        code:data.code,
                    },
                    success(e){
                        // console.log(e)
                        callback(e.data)
                    }
                })
            },
            fail(err){
                // console.log('login 接口调用失败，将无法正常使用开放接口等服务')   
                alert('接口调用失败');
            }
        })
    },
    relateUser(data){ //关联登录
        swan.showLoading({
            title: '加载中',
            mask:true
        });
        var that = this;
        return new Promise((resolve,reject)=>{
            swan.getUserInfo({
                success(res){
                    // console.log(data)
                    if(data){
                        swan.request({
                            url: that.globalData.urllogin + 'relateUser',
                            method: 'POST',
                            header: { 'content-type': 'application/x-www-form-urlencoded' },
                            data:{
                                openid:data.openid,
                                userInfo:JSON.stringify(res.userInfo)
                            },
                            success(e){
                                console.log(e.data)
                                that.globalData.userInfo=e.data;
                                resolve(e.data);
                            }
                        });
                    }else{
                        swan.getStorage({
                            key: 'login_data',
                            success: e => {
                                swan.request({
                                    url: that.globalData.urllogin + 'relateUser',
                                    method: 'POST',
                                    header: { 'content-type': 'application/x-www-form-urlencoded' },
                                    data:{
                                        openid:e.data.openid,
                                        userInfo:JSON.stringify(res.userInfo)
                                    },
                                    success(e){
                                        // console.log(e)
                                        that.globalData.userInfo=e.data;
                                        resolve(e.data);
                                    }
                                });
                            },
                            fail(){
                                that.getopenid(function(res){
                                    swan.setStorage({
                                        key: 'login_data',
                                        data: res,
                                        success:e=>{
                                            that.relateUser(res)
                                        }
                                    });
                                });
                            }
                        });             
                    }
                },
                fail(err){
                    reject(err);
                }
            })
        })
    },
    onShow(options) {
        // do something when show
    },
    onHide() {
        // do something when hide
    }
});
